package com.axin.blog.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.axin.blog.api.domain.Blog;
import com.axin.blog.api.domain.BlogComment;
import com.axin.blog.api.domain.WebUser;
import com.axin.blog.api.vo.BlogCommentVo;
import com.axin.blog.mapper.BlogCommentMapper;
import com.axin.blog.mapper.BlogMapper;
import com.axin.blog.service.IBlogCommentService;
import com.axin.blog.service.IBlogService;
import com.axin.blog.service.IWebUserService;
import com.axin.common.core.domain.web.TableDataInfo;
import com.axin.common.mybatis.core.ServicePlusImpl;
import com.axin.common.mybatis.page.PagePlus;
import com.axin.common.mybatis.utils.PageUtils;
import com.axin.common.security.model.LoginUser;
import com.axin.common.security.utils.SecurityUtils;
import com.axin.system.api.domain.SysMenu;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 评论 服务层实现
 *
 * @Author axin
 * @Date 2021/10/29 17:08
 **/
@Service
public class BlogCommentServiceImpl extends ServicePlusImpl<BlogCommentMapper, BlogComment, BlogCommentVo> implements IBlogCommentService {

    @Resource
    private IBlogCommentService commentService;

    @Resource
    private BlogMapper blogMapper;

    @Resource
    private IWebUserService webUserService;

    /**
     * 根据参数分页查询博客评论列表
     *
     * @param comment
     * @return
     */
    @Override
    public TableDataInfo<BlogComment> selectPageBlogCommentList(BlogComment comment) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (!loginUser.getUser().isAdmin()) {
            comment.setAuthorId(loginUser.getUserId());
        }
        return PageUtils.buildDataInfo(baseMapper.selectPageBlogCommentList(PageUtils.buildPage(), comment));
    }

    /**
     * 根据ID查询评论信息
     *
     * @param commentId
     * @return
     */
    @Override
    public BlogComment selectBlogCommentById(Long commentId) {
        return baseMapper.selectById(commentId);
    }

    /**
     * 批量删除评论
     *
     * @param commentIds 需要删除的评论ID
     * @return 结果
     */
    @Override
    public void deleteBlogCommentByIds(Long[] commentIds) {

        baseMapper.deleteBatchIds(Arrays.asList(commentIds));

    }


    /************************前台接口******************************************************************************************/


    /**
     * 查询博客评论列表
     *
     * @param blogId
     * @return
     */
    @Override
    public TableDataInfo<BlogCommentVo> selectBlogCommentList(Long blogId) {

        // 分页查询一级评论
        LambdaQueryWrapper<BlogComment> lqw = new LambdaQueryWrapper<BlogComment>()
                .eq(BlogComment::getBlogId, blogId)
                .eq(BlogComment::getParentId, 0);
        PagePlus<BlogComment, BlogCommentVo> result = pageVo(PageUtils.buildPagePlus(), lqw);
        for (BlogCommentVo parentVo : result.getRecordsVo()) {
            // 设置一级评论基本回显信息
            WebUser webUser = webUserService.selectWebUserById(parentVo.getUserId());

            parentVo.setNickName(webUser.getNickName());
            parentVo.setAvatar(webUser.getAvatar());

            // 查询子评论
            lqw = new LambdaQueryWrapper<BlogComment>()
                    .eq(BlogComment::getBlogId, blogId)
                    .eq(BlogComment::getParentId, parentVo.getCommentId())
                    .orderByAsc(BlogComment::getCreateTime);
            List<BlogCommentVo> children = this.listVo(lqw);

            // 设置一级评论基本回显信息
            for (BlogCommentVo child : children) {
                webUser = webUserService.selectWebUserById(child.getUserId());
                child.setNickName(webUser.getNickName());
                child.setAvatar(webUser.getAvatar());
                // 如果是回复一级评论不显示被回复人昵称
                child.setToNickName(webUserService.selectWebUserById(child.getToUserId()).getNickName());
            }
            parentVo.setChildren(children);

        }

        return PageUtils.buildDataInfo(result);

    }


    /**
     * 新增博客评论
     *
     * @param comment
     * @return 结果
     */
    @Override
    public int insertBlogComment(BlogComment comment) {
        return baseMapper.insert(comment);
    }
}
